package com.wallace.downloadserver.controller.open_api

import com.wallace.downloadserver.wrapper.DecryptRequestWrapper
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController
import java.io.InputStream
import javax.servlet.http.HttpServletRequest

@RestController
@RequestMapping(value = ["/downloadserver/open_api/maintain"], produces = ["application/json; charset=utf-8"])
class MaintainController {

    private val log: Logger = LoggerFactory.getLogger(MaintainController::class.java)

    @RequestMapping(value = ["/testUploadSpeed"], method = [RequestMethod.POST])
    fun testUploadSpeed(decryptRequestWrapper: DecryptRequestWrapper) {
        val size = decryptRequestWrapper.getSize()
        if (size > 0) {

        } else {
            log.debug("upload data size = $size.End test!")
        }
    }

    @RequestMapping(value = ["/testDownloadSpeed"], method = [RequestMethod.GET])
    fun testDownloadSpeed() {

    }

    @RequestMapping(value = ["/tempTestUploadSpeed"], method = [RequestMethod.POST])
    fun tempTestUploadSpeed(httpServletRequest: HttpServletRequest) {
        var inputStream: InputStream? = null
        try {
            inputStream = httpServletRequest.inputStream
            val buffer = ByteArray(128)
            var total = 0L
            var len = inputStream.read(buffer)
            while (len != -1) {
                total += len
                log.debug("total = $total")
                len = inputStream.read(buffer)
            }
        } catch (e: Exception) {
            log.warn("$e")
        } finally {
            inputStream?.close()
        }
    }

}